package com.lizemin.websocketserver.config;

import com.lizemin.websocketserver.handler.SimpleWebSocketServerHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * websocket配置
 *
 * @author lizemin
 * @version 1.0
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    /**
     * 设置websocket端点路径 ，并设置允许跨域
     *
     * @param registry 注册器
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(websocketServerHandler(), "/ws")
                .setAllowedOrigins("*"); // 允许跨域
    }

    @Bean
    public SimpleWebSocketServerHandler websocketServerHandler() {
        return new SimpleWebSocketServerHandler();
    }

}
